﻿using Fetches.Fetch.Dto;
using FetchesDeploy.Distributed.Factory;
using Microsoft.AspNetCore.Mvc;

namespace FetchesDeploy.ScalableFactoryUnit.Controllers
{
    /// <summary>
    /// 因为Container处在控制中心管理下只会进行Fetcher的Dispatch和Dismiss而不会涉及FetchTask这个层面的操作
    /// 所以Container中FetchTask层面的操作不考虑与控制中心任务分配的冲突是合理的
    /// </summary>
    [Route("[controller]")]
    [ApiController]
    public class FetchController : ControllerBase
    {
        [HttpGet]
        public string Get()
        {
            return "fetch api is working";
        }

        [HttpPost]
        public FetchReceipt Submit([FromBody]FetchOrder order)
        {
           return FetchFactoryUnit.Instance().TakeFetchOrder(order);
        }

        [HttpPost("{taskId}")]
        public ActionResult Continue(long taskId)
        {
            FetchFactoryUnit.Instance().ContinueFetch(taskId);
            return Ok($"fetch {taskId} on {FetchFactoryUnit.Instance().FactoryId} is working");
        }

        [HttpDelete("{taskId}")]
        public ActionResult Stop(long taskId)
        {
            FetchFactoryUnit.Instance().StopFetch(taskId);
            return Ok($"fetch {taskId} on {FetchFactoryUnit.Instance().FactoryId} is stopped");
        }
    }
}
